package com.codingke.codingbookstore.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.codingke.codingbookstore.R;
import com.codingke.codingbookstore.utils.AppUtils;
import com.codingke.codingbookstore.vo.User;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobSMS;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.CountListener;
import cn.bmob.v3.listener.RequestSMSCodeListener;
import cn.bmob.v3.listener.ResetPasswordByCodeListener;

public class ResetPasswordActivity extends BaseActivity {

    private EditText editText_phone,editText_validate_number,editText_password_new;
    private ImageView imageView_show_password;

    private boolean showPassword = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reset_password);
        editText_phone = (EditText) findViewById(R.id.editText_phone);
        editText_validate_number = (EditText) findViewById(R.id.editText_validate_number);
        editText_password_new = (EditText) findViewById(R.id.editText_password_new);
        imageView_show_password = (ImageView) findViewById(R.id.imageView_show_password);
    }

    public void resetPasswordClick(View view){
        AppUtils.hideInputMethod(view);
        String phone = editText_phone.getText().toString();
        if(TextUtils.isEmpty(phone)){
            showToast("请输入手机号码");
            return;
        }
        String password = editText_password_new.getText().toString();
        if(TextUtils.isEmpty(password)){
            showToast("请输入新密码");
            return;
        }
        String validateNumber = editText_validate_number.getText().toString();
        if(TextUtils.isEmpty(validateNumber)){
            showToast("请输入验证码");
            return;
        }
        BmobUser.resetPasswordBySMSCode(this,validateNumber,password, new ResetPasswordByCodeListener() {
            @Override
            public void done(BmobException ex) {
                if(ex==null){
                    showToast("密码重置成功");
                    finish();
                }else{
                    showToast("重置失败");
                }
            }
        });
    }

    public void getValidateNumberClick(View view){
        AppUtils.hideInputMethod(view);
        final String phone = editText_phone.getText().toString();
        if(TextUtils.isEmpty(phone)){
            showToast("请输入手机号码");
            return;
        }

        BmobQuery<User> query = new BmobQuery<>();
        query.addWhereEqualTo("mobilePhoneNumber",phone);
        query.addWhereEqualTo("username",phone);
        query.count(this, User.class, new CountListener() {
            @Override
            public void onSuccess(int i) {
                if(i==1){
                    BmobSMS.requestSMSCode(ResetPasswordActivity.this, phone, "coding_sms", new RequestSMSCodeListener() {
                        @Override
                        public void done(Integer integer, BmobException e) {
                            if(e==null){//验证码发送成功
                                showToast("验证码发送成功,请稍候查看短信");
                            }
                        }
                    });
                }else{
                    showToast("手机号码不存在,请重新注册");
                    startActivity(new Intent(ResetPasswordActivity.this,RegisterActivity.class));
                    finish();
                }
            }

            @Override
            public void onFailure(int i, String s) {

            }
        });


    }

    public void showHidePasswordClick(View view){
        AppUtils.hideInputMethod(view);
        if (showPassword){
            imageView_show_password.setSelected(true);
            editText_password_new.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            showPassword = false;
        }else{
            imageView_show_password.setSelected(false);
            editText_password_new.setTransformationMethod(PasswordTransformationMethod.getInstance());
            showPassword = true;
        }
    }
}
